package cn.itsource.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PetConstant;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.SendEmailService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop,ShopQuery> implements IShopService {

    //基础方法由继承得到

    /**
     * 注入mapper
     */
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SendEmailService sendEmailService;

    @Autowired
    private RedisService redisService;

    /**
     * 先写主体,再写细节
     * @param shop 店铺对象
     *
     * 优化
     *    1.自定义异常
     *    2.加盐加密公共抽取(钩子函数)
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {

        //细节1,验证密码手动验证
        Employee admin = shop.getAdmin();
        if (admin == null){
            throw new BusinessException("店长信息为空");
        }

        if (!StringUtils.equals(admin.getPassword(),admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一样");
        }

        //细节2,手机号,邮箱,账号每个人3个字段都要绝对唯一
        List<Employee> employees = employeeMapper.selectByUsernameOrPhoneOrEmail(admin);
        //代码要扁平式管理
        if (employees.size() > 0){
            throw new BusinessException("账号重复,是手机号,邮箱,用户名其中一个有问题");
        }

        //主体:店铺入驻流程

        //1.新增员工(拿到员工自增长主键id)
        //1.1盐值入库 ,加密之后密码串入库
/*        String salt = IdUtil.fastSimpleUUID();
        admin.setSalt(salt);
        String md5Pwd = MD5.create().setSalt(salt.getBytes()).digestHex(admin.getPassword());
        admin.setPassword(md5Pwd);*/

        admin.initMd5();

        employeeMapper.insertSelective(admin);

        //2.把1步骤拿到的员工id设置到店铺对象的adminId管理员字段中
        shop.setAdmin_id(admin.getId());
        shopMapper.insertSelective(shop);

        //2.1新增店铺,拿到店铺的主键id
        admin.setShopId(shop.getId());
        //3.把2.1拿到的店铺主键id设置给员工,修改员工对象
        employeeMapper.updateByPrimaryKeySelective(admin);


        //--------------------------发送邮件-------------------------
        //1.发送邮件
        String uuid = IdUtil.fastSimpleUUID();
        //sendEmailService.send("宠物乐园激活","请激活您的宠物乐园平台店铺,点击链接,http://localhost/shop/active/"+uuid+"/"+shop.getId(),admin.getEmail());

        //2.把uuid作为key,店铺id作为value存到redis里面
        redisService.setStringKeyAndValue(20,uuid,shop.getId());
    }

    @Override
    public Object activeShop(String uuid,String shopId) {
        //0.通过shopId去数据库查询,如果状态是3,就提示已激活成功
        Shop shop1 = shopMapper.selectByPrimaryKey(shopId);

        if (PetConstant.Shop.TO_BE_ACTIVE.equals(shop1.getState())){
            throw new BusinessException("已经激活过了,不要重复季候");
        }

        //1.根据uuid从redis里面拿到数据, 如果拿不到,代表激活过期了
        String redisShopId = redisService.getKeyStringValue(uuid);
        if (StringUtils.isBlank(redisShopId)){
            throw new BusinessException("店铺注册时间已过,请联系管理员裕皇大帝");
        }
        //2.如果拿到了店铺id,修改state

        Shop shop = new Shop();
        shop.setId(Long.valueOf(redisShopId));
        shop.setState(PetConstant.Shop.TO_BE_ACTIVE);

        shopMapper.updateByPrimaryKeySelective(shop);
        return AjaxResult.createSuccess();

    }

    /*
        1cf5ac837b83863a2afc57601c32603c five
     */
    public static void main(String[] args) {

        String s2 = IdUtil.fastSimpleUUID();
        String s21 = IdUtil.fastSimpleUUID();
        System.out.println(s2);
        System.out.println(s21);

        String s = MD5.create().setSalt("123456".getBytes()).digestHex("123456");
        String s1 = MD5.create().setSalt("84646f4d085c41cfa401f7233f8a564f".getBytes()).digestHex("123");
        System.out.println(s);
        System.out.println(s1);

        String a = "aaa"; //常量池里面
        String b = "aaa";
        System.out.println(a==b);
        String a1 = new String("aaa");//堆
        String b1 = new String("aaa");
        System.out.println(a==b);
        System.out.println(a1==b1);

        // == 和equelsqubie

        //== 比较地址
    }
}